package dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.front.mongdb.Comments;
import com.dongdongshop.front.service.MongdbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Component
@Transactional
public class MongdbServiceImpl implements MongdbService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<Comments> selectMongoDB(Long goodId) {
        Sort sort = Sort.by(Sort.Order.desc("dateTime"));
        Query query = Query.query(Criteria.where("goodId").is(goodId));
        Pageable pageable = PageRequest.of(0,10,sort);
        return mongoTemplate.find(query.with(pageable), Comments.class);
    }
}
